package com.sbtr.business.classify.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.classify.dto.AlgoTemplateConditionDto;
import com.sbtr.business.classify.entity.AlgoCondition;
import com.sbtr.business.classify.entity.AlgoTemplateCondition;
import com.sbtr.business.classify.mapper.AlgoTemplateConditionMapper;
import com.sbtr.business.classify.service.AlgoConditionService;
import com.sbtr.business.classify.service.AlgoTemplateConditionService;
import com.sbtr.business.classify.vo.AlgoConditionSaveVO;
import com.sbtr.common.PageDto;
import com.sbtr.util.PageDtoUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 裁剪条件关联 Service接口实现类
 *
 * @author dgl
 * @since  2023-09-01
 */
@Transactional
@Service
public class AlgoTemplateConditionServiceImpl extends ServiceImpl<AlgoTemplateConditionMapper,AlgoTemplateCondition> implements AlgoTemplateConditionService {

    @Autowired
    AlgoTemplateConditionMapper algoTemplateConditionMapper;
    @Autowired
    AlgoConditionService algoConditionService;

    @Override
    public PageDto<AlgoTemplateCondition> pageList(AlgoTemplateConditionDto algoTemplateConditionDto) {

        IPage<AlgoTemplateCondition> page = new Page<>();
        page.setSize(algoTemplateConditionDto.getPageSize());
        page.setCurrent(algoTemplateConditionDto.getPage());
        page = algoTemplateConditionMapper.selectPages(page, BeanUtil.beanToMap(algoTemplateConditionDto));
        PageDto<AlgoTemplateCondition> r = PageDtoUtils.getPageDtoByIPage(page, AlgoTemplateCondition.class);

        return r;
    }

    @Override
    public void batchOptBfClearCondition(List<AlgoConditionSaveVO> newList, Long algoTemplateVersionId,Long algoTemplateId) {
        QueryWrapper<AlgoTemplateCondition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("algo_template_version_id",algoTemplateVersionId);
        List<AlgoTemplateCondition> old= algoTemplateConditionMapper.selectList(queryWrapper);

        if(CollectionUtils.isNotEmpty(old)){
            //清除设置条件
            List<Long> conditionIds = old.stream().map(AlgoTemplateCondition::getAlgoConditionId).collect(Collectors.toList());
            algoConditionService.removeByIds(conditionIds);

            //清除设置关联
            UpdateWrapper<AlgoTemplateCondition> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("algo_template_version_id",algoTemplateVersionId);
            algoTemplateConditionMapper.delete(updateWrapper);
        }

        //设置条件
        List<AlgoCondition> algoConditionList = new ArrayList<>();
        for (AlgoConditionSaveVO algoConditionSaveVO :newList) {
            for (String param :algoConditionSaveVO.getParametersList()) {
                AlgoCondition algoCondition = new AlgoCondition();
                algoCondition.setName(algoConditionSaveVO.getName());
                algoCondition.setParameters(param);
                algoConditionList.add(algoCondition);
            }
        }
        algoConditionService.saveBatch(algoConditionList);

        //设置关联
        List<AlgoTemplateCondition> list = new ArrayList<>();
        for (AlgoCondition algoCondition:algoConditionList) {
            AlgoTemplateCondition algoTemplateCondition = new AlgoTemplateCondition();
            algoTemplateCondition.setAlgoConditionId(algoCondition.getId());
            algoTemplateCondition.setAlgoTemplateId(algoTemplateId);
            algoTemplateCondition.setAlgoTemplateVersionId(algoTemplateVersionId);
            list.add(algoTemplateCondition);
        }

        this.saveBatch(list);

    }
}

